/**
 * Magic ComboBox Grid Plugin v1.0.1
 * author: Mac_J@boarsoft.com
 * need combo.js, core.js
 */
mac.combo_grid = function(self, cfg) {
	var hd = self.children('.head'), tf = hd.find('input')
		, btn = hd.children('.btn'), ico = btn.children('.icon')
		, bd = self.children('.body'), gd = self.config.boxEl;
	self.key = '';
	ico.removeClass('icon-triangle-1-s').addClass('icon-search');
	self.onOpen = cfg.onOpen;
	cfg = $.extend(self.config, cfg, {
		onOpen: function(me){
			gd.adjust(bd);
			gd.select(me.selected);
			if (self.onOpen) {
				self.onOpen();
			}
//			ico.removeClass('icon-triangle-1-s').addClass('icon-search');
		}
//		onClose: function(me){
//			ico.removeClass('icon-search').addClass('icon-triangle-1-s');
//		},
//		onSelect: function(){
//			self.key = tf.val();
//		}
	});
	$.extend(gd.config, {
		onRowClick: function(e){
			var ec = $(this), k = ec.attr('name');
			if (cfg.multiSelect) {
				if (gd.selected && ec.hasClass('selected')) {
					delete gd.selected[k];
					ec.removeClass('selected');
				} else {
					gd.selected = gd.selected || {};
					gd.selected[k] = gd.data[k];
					ec.addClass('selected');
				}
				var sl = [];
				$.each(gd.selected, function(n, c){
					sl.push(c);
				});
				self.select(sl);
			} else {
				gd.find('.tr').removeClass('selected');
				ec.addClass('selected');
				gd.selected = {};
				gd.selected[k] = gd.data[k];
				self.select(gd.data[k]);
				self.close();
			}
		},
		afterLoad: function(dd, po, me){
			self.data = dd;
			gd.select(self.selected);
			gd.find('.tr').first().addClass('hover');
		}
	});
	gd.select = function(sl){
		//sl = self.checkin(sl);
		var at = gd.find('.tr').removeClass('selected');
		$.each(sl, function(n, k){
			at.filter("[name='" + k + "']").addClass('selected');
		});
	}
	tf.unbind('keydown').keydown(function(e) {
		var k = e.keyCode, op = self.isOpen
			, rs = gd.find('.tr'), v = tf.val();
		switch (k) {
		case 9: //TAB
		case 27:// ESC
			self.close();
			tf.focus();
			break;
		case 38: // UP
		case 40: // DOWN
			var d = (k == 40), h = rs.filter('.hover')
				, s = rs.filter('.selected'), x, t, z;
			if (!op && d) {
				self.key = v;
				self.open();
			}
			if (h.length) {
				h = h.removeClass('hover').first();
				x = d? h.next().next():h.prev().prev();
			}
			if (!x || !x.length) {
				x = d? rs.first() : rs.last();
			}
			if (x.length) {
				x.addClass('hover');
				var yb = gd.find('.ybar');
				t = x.position().top;
				if (t < 28) {
					yb.scrollTop(yb.scrollTop() + t - 28);
				} else if (t > 253) {
					yb.scrollTop(yb.scrollTop() + t - 253);
				}
			}
			return false;
		case 13:// RETURN
			var h = rs.filter('.hover');
			if (op && h.length && v == self.key) {
				h.click();
				h.removeClass('hover');
				return false;
			}
			if (!op) {
				self.open();
			}
			if (v != self.key) {
				self.key = v;
				self.selected = [];
				gd.load({ key: v });
			}
			return false;
		}
		return true;
	});
	btn.unbind('click').click(function(){
		if (self.hasClass('disabled')) {
			return;
		}
		if (!self.isOpen) {
			self.open();
		}
		var v = tf.val();
		if (v == self.key) {
			return false;
		}
		self.key = v;
		delete self.selected;
		delete self.displayValue;
		gd.load({ key: v });
		return false;
	});
	return self;
}